﻿using System;
using System.Collections.Generic;
using System.Text;

using Danser_Control.Managers;
using Danser_Entity.Entities;
using Danser_Entity.Queries.Data;

namespace Danser_Control.ManagersImpl
{
    /// <summary>
    /// implements the IValidationManager interface. 
    /// </summary>
    public class ValidationManagerImpl : IValidationManager
    {
        #region Private Fields

        private IDBManager manager = new DBManager();

        #endregion

        #region IValidationManager Members

        /// <summary>
        /// creates a validation template and adds it to the DB
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="apliedOnDefault"></param>
        /// <param name="mailList"></param>
        /// <param name="ruleExpression"></param>
        public void CreateTemplate(string templateName, bool apliedOnDefault, string mailList, string ruleExpression)
        {
            ValidationTemplate template = new ValidationTemplate(
                templateName,
                apliedOnDefault,
                mailList,
                ruleExpression);
            manager.AddValidationTemplate(template);
        }

        /// <summary>
        /// creates a validation template and adds it to the DB
        /// </summary>
        /// <param name="validationTemplate"></param>
        public void CreateTemplate(ValidationTemplate validationTemplate)
        {
            CreateTemplate(
                validationTemplate.Name,
                validationTemplate.AppliedOnDefault,
                validationTemplate.MailList,
                validationTemplate.Expression);
        }

        /// <summary>
        /// removes a validation template from the DB
        /// </summary>
        /// <param name="templateID">
        /// unique template identifier
        /// </param>
        public void RemoveValidationTemplate(int templateID)
        {
            manager.DeleteValidationTemplate(templateID);
        }

        /// <summary>
        /// modifies template details
        /// </summary>
        /// <param name="templateID"></param>
        /// <param name="MailList"></param>
        /// <param name="appliedOnDefault"></param>
        public void ModifyTemplateDetails(int templateID, String MailList, bool appliedOnDefault)
        {
            manager.ModifyTemplateDetails(templateID,MailList,appliedOnDefault);
        }

        /// <summary>
        /// returns a list of validation templates
        /// </summary>
        /// <param name="templateIds">
        /// a list of unique template identifiers to retrieve
        /// </param>
        /// <returns>
        /// ValidationTemplate list
        /// </returns>
        public IList<ValidationTemplate> GetValidationTemplates(IList<int> templateIds)
        {
            String filter = "";

            // list is empty or null - return all templates:
            if (null == templateIds || 0 == templateIds.Count)
            {
                return manager.GetValidationTemplates(null);
            }
            foreach (int ID in templateIds)
            {
                filter += String.Format(DBConstants.VALIDATION_TEMPLATES_ID + " = {0} "+ManagerConstants.OR_OPERATOR+"", ID);
            }
            filter = filter.Remove(filter.Length - (2 + ManagerConstants.OR_OPERATOR.Length));

            return manager.GetValidationTemplates(filter);
        }

        /// <summary>
        /// returns a list of validation templates
        /// </summary>
        /// <param name="filter">
        /// a string in SQL syntax representing what templates to retrieve
        /// </param>
        /// <returns>
        /// ValidationTemplate list
        /// </returns>
        public IList<ValidationTemplate> GetValidationTemplates(String filter)
        {
            return manager.GetValidationTemplates(filter);
        }

        /// <summary>
        /// return test info of all the tests that failed a template
        /// </summary>
        /// <param name="validationTemplateID">
        /// unique template identifier
        /// </param>
        /// <returns>
        /// TestInfo list
        /// </returns>
        public List<TestInfo> GetFailedTestForTemplate(int validationTemplateID)
        {
            return manager.GetFailedTestForTemplate(validationTemplateID);
        }

        /// <summary>
        /// returns all templates that a test failed
        /// </summary>
        /// <param name="testInfoIndex">
        /// unique test identifier
        /// </param>
        /// <returns>
        /// ValidationTemplate list
        /// </returns>
        public List<ValidationTemplate> GetFailingTemplatesForTest(String testInfoIndex)
        {
            return manager.GetFailingTemplatesForTest(testInfoIndex);
        }

        #endregion
    }
}
